.PHONY: build test clean install lint

BIN_NAME := onyx-snapshotter
BIN_PATH := ./bin/$(BIN_NAME)
VERSION := $(shell git describe --tags --always --dirty 2>/dev/null || echo "dev")
BUILD_TIME := $(shell date -u +"%Y-%m-%dT%H:%M:%SZ")
GIT_COMMIT := $(shell git rev-parse --short HEAD 2>/dev/null || echo "unknown")

LDFLAGS := -ldflags "-X main.Version=$(VERSION) -X main.BuildTime=$(BUILD_TIME) -X main.GitCommit=$(GIT_COMMIT)"

build:
	@echo "Building $(BIN_NAME)..."
	CGO_ENABLED=1 go build $(LDFLAGS) -o $(BIN_PATH) ./cmd/onyx-snapshotter

test:
	@echo "Running tests..."
	go test -v -race -coverprofile=coverage.out ./...
	go tool cover -html=coverage.out -o coverage.html

test-short:
	go test -v -short ./...

lint:
	@echo "Linting..."
	golangci-lint run ./...

clean:
	@echo "Cleaning..."
	rm -f $(BIN_PATH)
	rm -f coverage.out coverage.html
	go clean -cache -testcache

install: build
	@echo "Installing..."
	cp $(BIN_PATH) /usr/local/bin/

docker-build:
	docker build -t onyx-snapshotter:$(VERSION) .

docker-run:
	docker run -it \
		-v /var/lib/onyx-snapshotter:/var/lib/onyx-snapshotter \
		-v /run/onyx-snapshotter:/run/onyx-snapshotter \
		onyx-snapshotter:$(VERSION)

deps:
	go mod download
	go mod tidy

fmt:
	gofumpt -l -w .

dev-setup:
	@echo "Setting up development environment..."
	go install mvdan.cc/gofumpt@latest
	@echo "Development tools installed"

help:
	@echo "Available targets:"
	@echo "  make build         - Build the binary"
	@echo "  make test          - Run tests"
	@echo "  make lint          - Run linter"
	@echo "  make clean         - Clean build artifacts"
	@echo "  make install       - Install binary"
	@echo "  make docker-build  - Build Docker image"
	@echo "  make fmt           - Format code"
